package org.qiuqiu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.qiuqiu.dao.UserDao;
import org.qiuqiu.pojo.User;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 杜秋野
 * 
 */
public class UserAction extends ActionSupport implements ServletResponseAware,
		ServletRequestAware {

	HttpServletRequest request;
	HttpServletResponse response;
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	/**
	 * 新用户注册
	 * 
	 * @throws IOException
	 */
	public void regist() throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			UserDao ud = new UserDao();
			user.setUserId(ud.insert(user));
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			out.print("true");
		} catch (SQLException e) {
			e.printStackTrace();
			out.print("false");
		}

	}

	/**
	 * 用户登录
	 * 
	 * @return
	 */
	public String login() {
		UserDao ud = new UserDao();
		User u = ud.checkUserPassword(user.getUserName());
		HttpSession session = request.getSession();
		session.setAttribute("error1", null);
		session.setAttribute("error2", null);
		if (u == null) {
			session.setAttribute("error1", "用户名不存在!");
			return "input";
		} else {
			if (user.getPassword().equals(u.getPassword())) {
				user = u;
				session.setAttribute("user", user);
				session.setMaxInactiveInterval(1200);
				return SUCCESS;
			} else {
				session.setAttribute("error2", "密码错误!");
				return INPUT;
			}
		}
	}

	/**
	 * 用户退出
	 * 
	 * @return
	 */
	public String loginOut() {
		HttpSession session = request.getSession();
		session.invalidate();
		return "exit";
	}

	/**
	 * 检测用户名是否存在
	 * 
	 * @throws Exception
	 */
	public void checkExistedUserName() throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		user.setUserName(java.net.URLDecoder
				.decode(user.getUserName(), "UTF-8"));
		UserDao ud = new UserDao();
		User u = ud.checkUserPassword(user.getUserName());
		Boolean checkResult = true;
		if (u != null) {
			checkResult = false;
		} else {
			checkResult = true;
		}
		out.print(checkResult.toString());
	}

	/**
	 * 修改用户信息
	 * 
	 * @throws IOException
	 */
	public void UpdateUser() throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		User user1 = (User) session.getAttribute("user");
		user1.setFirstName(user.getFirstName());
		user1.setSecondName(user.getSecondName());
		user1.setSex(user.getSex());
		user1.setEmail(user.getEmail());
		UserDao ud = new UserDao();
		try {
			ud.modUser(user1);
			session.setAttribute("user", user1);
			out.print(true);
		} catch (SQLException e) {
			e.printStackTrace();
			out.print(false);
		}
	}

	/**
	 * 修改用户密码
	 * 
	 * @throws IOException
	 */
	public void changePassword() throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		User user1 = (User) session.getAttribute("user");
		user1.setPassword(user.getPassword());
		UserDao ud = new UserDao();
		try {
			ud.modUser(user1);
			session.setAttribute("user", user1);
			out.print(true);
		} catch (SQLException e) {
			e.printStackTrace();
			out.print(false);
		}
	}

	/**
	 * 修改密码保护问题
	 * 
	 * @throws IOException
	 */
	public void changeQuestion() throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		User user1 = (User) session.getAttribute("user");
		user1.setQuestion1(user.getQuestion1());
		user1.setQuestion2(user.getQuestion2());
		user1.setQuestion3(user.getQuestion3());
		user1.setAnswer1(user.getAnswer1());
		user1.setAnswer2(user.getAnswer2());
		user1.setAnswer3(user.getAnswer3());
		UserDao ud = new UserDao();
		try {
			ud.modUser(user1);
			session.setAttribute("user", user1);
			out.print(true);
		} catch (SQLException e) {
			e.printStackTrace();
			out.print(false);
		}
	}
}